define(["backbone", "framework","layer_m", "_global","_clearCart", "swiper",  "_cartData"],
    function (Backbone, framework,layer,_gl,_clearCart,Swiper, _CD) {
    return Backbone.View.extend({
        el: '#floor2',
        events: {
            "click .reduce":"reduce",
            "click .add": "add",
            "click #addToCart":"addToCart"
        },
        initialize: function () {
          
        },
        render: function (categoryid, pno) {
            var scope = this;
            scope.categoryid = categoryid;
            scope.pno = pno;
            $(scope.el).html('');
            framework.ajax({
                type:'GET',
                url: framework.getAjaxUrl('Details', ["gno"], [scope.pno]),
                success: function (d) {
                    var data = d.result;
                    console.log(data);
                    data.categoryid = scope.categoryid;
                    framework.renderTemplate({
                        el: $(scope.el),
                        templateName:"templateDetail",
                        data:data
                    });
                    _gl.backTop();
                    _gl.imgSrcReplace($("#prodSlider img.oneImg"));
                    var Psw = new Swiper('.swiper-container', {
                        autoplay: 3000,
                        pagination: '.swiper-pagination',
                        paginationClickable: true,
                        lazyLoading: true,
                        loop: true
                    });
                }
            });
        },
        //操作指定商品数量减1
        reduce: function (e) {
            var $this = $(e.target);
            var scope = this;
            if (parseInt(scope.getNumbEl().val()) <= 2) {
                $this.prop("disabled", true);
            } 
            scope.getNumbEl().val(parseInt(scope.getNumbEl().val()) - 1);
        },
        //操作指定商品数量加1
        add: function (e) {
            var $this = $(e.target);
            var scope = this;
            var $reduce = $this.parent().parent().find(".reduce");
            if ($reduce.prop("disabled")) {
                $reduce.prop("disabled", false);
            }
            scope.getNumbEl().val(parseInt(scope.getNumbEl().val()) + 1);
        },
        addToCart: function (e) {
            var $this = $(e.target);
            var scope = this;
            var data = {
                pno: $this.attr("data-pno"),
                price: $this.attr("data-price"),
                imgSrc: $this.attr("data-imgSrc"),
                categoryId: $this.attr("data-categoryId"),
                numb: scope.getNumbEl().val(),
                info: $this.attr("data-info")
            };
            var cartSave = function () {
                _CD.save(data);
                scope.setCartNumb();
                scope.getNavCartEl().val(1);
                scope.lastStepDialog();
            };
            
            if (_CD.isOverdue()) {
                _clearCart.clearCart({
                    clearCartFunc: function () {
                        _CD.clear();
                        cartSave();
                    },
                    unClearCartFunc: function () {
                        cartSave();
                    }
                });
                return false;
            }
            cartSave();
        },
        lastStepDialog: function () {
            var scope = this;
            var content = '购物车共' + _CD.getTotal() + '个商品　　   合计：' + _CD.getTotalPrice();
            var href = window.location.href;
            if (href.indexOf("#") > 0) {
                href = href.split("#")[0];
            }
            var layerIndex = _gl.confirm({
                title: '商品已成功添加到购物车！',
                content: content,
                btnName: ['去结算', '继续购物'],
                yes: function () {
                    layer.closeAll();
                    window.location.href = href + '#goods/' + scope.categoryid + '/detail';
                },
                no: function () {
                 //   layer.closeAll();
                    window.location.href = href + '#cart/cart';
                }
            });
        },
        // 获取商品数量的元素
        getNumbEl: function () {
            return $("#detailGoodsNumb");
        },
        getNavCartEl: function () {
            return $("#cartNumb");
        },
        //设置购物车中物品数量
        setCartNumb:function(){
            //if (parseInt(_CD.getTotal()) <= 0) {
            //    this.getNavCartEl().html(parseInt(_CD.getTotal())).hide();
            //} else {
            //    this.getNavCartEl().html(parseInt(_CD.getTotal())).show();
            //}
            if (parseInt(_CD.getProductsTotal()) <= 0) {
                this.getNavCartEl().html(parseInt(_CD.getProductsTotal())).hide();
            } else {
                this.getNavCartEl().html(parseInt(_CD.getProductsTotal())).show();
            }
        },
        showDetail:function(){
            $("#floor1").hide();
            $("#floor2").show();
        }
    });
});